﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Sispe.Mvc4.Controllers
{
    public class SancionController : Controller
    {
        [Dependency]
        public ISancionFaltasBL sancionBL { get; set; }

        [Dependency]
        public IContratoBL contratoBL { get; set; }

        [Dependency]
        public ITipoSancionBL tipoSancionBL { get; set; }

        public ActionResult Index(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Contrato");
            }
            else
            {
                var sanciones = (from c in sancionBL.GetAllFromSancionFalta()
                                   where c.IdContrato.Equals(id)
                                   select c).ToList();

                var contrato = contratoBL.GetFromContratoById(id);
                if (contrato == null)
                {
                    return RedirectToAction("Index", "Contrato");
                }

                ViewData["IdContrato"] = contrato.IdContrato;
                ViewData["TRABAJADOR"] = contrato.TRABAJADOR.PERSONA.NombreCompleto;

                return View(sanciones);
            }
        }


        public ActionResult Create(int id = 0)
        {
            var tipoSancion = tipoSancionBL.GetAllFromTipoSancion();
            ViewData["IdTipoSancion"] = new SelectList(tipoSancion, "IdTipoSancion", "Descripcion");

            ViewData["IdContrato"] = id;
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection colection)
        {
            var a = colection["FechaInicio"].ToString();
            var b = colection["FechaTermino"].ToString();
            var FechaInicio = a.Substring(3, 2) + "-" + a.Substring(0, 2) + "-" + a.Substring(6, 4);
            var FechaTermino = b.Substring(3, 2) + "-" + b.Substring(0, 2) + "-" + b.Substring(6, 4);            


            try
            {
                SANCION_FALTA sancion = new SANCION_FALTA()
                {
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    FechaInicio = Convert.ToDateTime(FechaInicio).Date,
                    FechaTermino = Convert.ToDateTime(FechaTermino).Date,
                    IdTipoSancion = Int32.Parse(colection["IdTipoSancion"].ToString()),
                    Motivo = colection["Motivo"].ToString(),
                };

                sancionBL.AddSancionFalta(sancion);
                return RedirectToAction("Index", "Sancion", new { @id = sancion.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return RedirectToAction("Create");
            }
        }


        public ActionResult Edit(int id = 0)
        {

            if (id == 0)
            {
                return View();
            }

            var currSancion = new SANCION_FALTA();

            var sancion = (from c in sancionBL.GetAllFromSancionFalta() where c.IdSancion.Equals(id)
                             select c).ToList();

            foreach (var item in sancion.Where(c => c.IdSancion.Equals(id)))
            {
                var fecIniBD = item.FechaInicio.Value.ToShortDateString();
                var fecFinBD = item.FechaTermino.Value.ToShortDateString();

                ViewData["FechaInicio"] = fecIniBD.Substring(3, 2) + "-" + fecIniBD.Substring(0, 2) + "-" + fecIniBD.Substring(6, 4);
                ViewData["FechaTermino"] = fecFinBD.Substring(3, 2) + "-" + fecFinBD.Substring(0, 2) + "-" + fecFinBD.Substring(6, 4);

                currSancion.IdSancion = item.IdSancion;
                currSancion.IdContrato = item.IdContrato;
                currSancion.Motivo = item.Motivo;                
                currSancion.IdTipoSancion = item.IdTipoSancion;
                
            }

            if (currSancion == null)
            {
                return RedirectToAction("Index", "Contrato");
            }

            var tipoS = tipoSancionBL.GetAllFromTipoSancion();

            ViewData["IdTipoSancion"] =
                new SelectList(tipoS, "IdTipoSancion", "Descripcion", currSancion.IdTipoSancion);

            return View(currSancion);
        }


        [HttpPost]
        public ActionResult Edit(int id, FormCollection colection)
        {
            var a = colection["FechaInicio"].ToString();
            var b = colection["FechaTermino"].ToString();
            var FechaInicio = a.Substring(3, 2) + "-" + a.Substring(0, 2) + "-" + a.Substring(6, 4);
            var FechaTermino = b.Substring(3, 2) + "-" + b.Substring(0, 2) + "-" + b.Substring(6, 4);            


            try
            {
                SANCION_FALTA sancion = new SANCION_FALTA()
                {
                    IdSancion = id,
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    FechaInicio = Convert.ToDateTime(FechaInicio).Date,
                    FechaTermino = Convert.ToDateTime(FechaTermino).Date,
                    IdTipoSancion = Int32.Parse(colection["IdTipoSancion"].ToString()),
                    Motivo = colection["Motivo"].ToString(),
                };

                sancionBL.UpdateSancionFalta(sancion);
                return RedirectToAction("Index", "Sancion", new { @id = sancion.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return View();
            }
        }



    }
}
